﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using BLL;

public partial class Report_PlatformSales : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("900100");
        if (!IsPostBack)
        {
            BindDropOrderFrom();
            //BindSales();
        }

    }

    private void BindDropOrderFrom()
    {
        List<Model.ORDERFROM> lst = new BLL.ORDERFROM().GetAllList(false);
        drpOrderFrom.Items.Add(new ListItem("全部", "-1"));
        foreach (Model.ORDERFROM order in lst)
        {
            drpOrderFrom.Items.Add(new ListItem(order.OF_NAME, order.OF_CODE));
        }
    }

    private void BindSales(bool isExport)
    {
        #region 组织日期条件
        string sqlstr = "";
        sqlstr += drpOrderFrom.SelectedValue == "-1" ? "" : " and ORDERFROM.OF_CODE='" + drpOrderFrom.SelectedValue + "' ";
        if (txtStart.Text.Trim().Length > 0)
        { sqlstr += " AND ORDER_CREATEDATE >= '" + txtStart.Text.Trim() + "'"; }
        if (txtEnd.Text.Trim().Length > 0)
        {
            sqlstr += " AND ORDER_CREATEDATE<'" + DateTime.Parse(txtEnd.Text.Trim()).AddDays(1).ToString() + "'";
        }
        if (txtDate1.Text.Trim().Length > 0)
        { sqlstr += " AND ORDER_CHECKDATE >= '" + txtDate1.Text.Trim() + "' "; }
        if (txtDate2.Text.Trim().Length > 0)
        {
            sqlstr += " AND ORDER_CHECKDATE<'" + DateTime.Parse(txtDate2.Text.Trim()).AddDays(1).ToString() + "'";
        }
        if (txtDate3.Text.Trim().Length > 0)
        { sqlstr += " AND ORDER_SENDDATE >= '" + txtDate3.Text.Trim() + "'"; }
        if (txtDate4.Text.Trim().Length > 0)
        {
            sqlstr += " AND ORDER_SENDDATE<'" + DateTime.Parse(txtDate4.Text.Trim()).AddDays(1).ToString() + "'";
        }
        #endregion
        /*
        string sqlstr = "";
        sqlstr += drpOrderFrom.SelectedValue == "-1" ? "" : " and ORDERFROM.OF_CODE='" + drpOrderFrom.SelectedValue + "' ";
        sqlstr += txtStart.Text.Trim() == "" ? "" : " and V_ORDERDOC.ORDER_SENDDATE>='" + txtStart.Text.Trim() + "' ";
        sqlstr += txtEnd.Text.Trim() == "" ? "" : " and V_ORDERDOC.ORDER_SENDDATE<='" + txtEnd.Text.Trim() + "' ";
        */
        int recordCount;
        DataSet dst = new BLL.ORDERDOC().GetPlatformSales(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, sqlstr, out recordCount);

        if (isExport)
        {
            DataTable2Excel(dst.Tables[1]);
        }

        dlistSales.DataSource = dst.Tables[1];
        dlistSales.DataBind();
        //AspNetPager1.RecordCount = recordCount;
    }
    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            //dtData.Rows.RemoveAt(0);
            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();




            dtData.Columns["ID"].ColumnName = "序号";
            dtData.Columns["SENDDATE"].ColumnName = "日期 ";
            dtData.Columns["OF_CODE"].ColumnName = "平台代码";
            dtData.Columns["OF_NAME"].ColumnName = "平台名 ";
            dtData.Columns["AMOUNT"].ColumnName = "销售额";
            dtData.Columns["QTY"].ColumnName = "销售件数";
            //dtData.Columns["AMT_RATE"].ColumnName = "占比（金额）";
            //dtData.Columns["AVGPRICE"].ColumnName = "平均单价";
            //dtData.Columns["KW"].ColumnName = "仓库";
            //dtData.Columns["SL"].ColumnName = "库存量";
            //dtData.Columns["SL3"].ColumnName = "可用量";
            //dtData.Columns["PERIOD"].ColumnName = "周转期";
            //for (int i = 0; i < dtData.Rows.Count; i++)
            //{
            //    //dtData.Rows[i]["发货/入库"] = dtData.Rows[i]["SendOrReceive"].ToString() == "1" ? "已发货" : "未发货"; 

            //    dtData.Rows[i]["发放状态"] = dtData.Rows[i]["SENDFLAG"].ToString() == "1" ? "已发放" : "未发放";

            //}
            //////移除列
            //dtData.Columns.Remove(dtData.Columns["SENDFLAG"]); 

            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    } 

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if ((txtStart.Text.Trim().Length < 1 || txtEnd.Text.Trim().Length < 1) && (txtDate1.Text.Trim().Length < 1 || txtDate2.Text.Trim().Length < 1) && (txtDate3.Text.Trim().Length < 1 || txtDate4.Text.Trim().Length < 1))
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert(\"请至少输入一个起止日期\");", true);
            return;
        }
        BindSales(false);
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        if ((txtStart.Text.Trim().Length < 1 || txtEnd.Text.Trim().Length < 1) && (txtDate1.Text.Trim().Length < 1 || txtDate2.Text.Trim().Length < 1) && (txtDate3.Text.Trim().Length < 1 || txtDate4.Text.Trim().Length < 1))
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert(\"请至少输入一个起止日期\");", true);
            return;
        }
        BindSales(true );
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindSales(false);
    }

    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
}
